The MutableSharedFlow() factory function has an optional replay parameter. This indicates how many objects should be cached by the flow and delivered to late subscribers. The default is 0, so nothing gets cached this way.

Here, we have the same random-numbers SharedFlow from before, except that replay is set to 2. Also, the "B" subscriber delays its collect() call by one second. The result is that our MutableSharedFlow will emit five objects before "B" subscribes. Due to the replay value, though, "B" will immediately get the last two of those five objects. After that, "B" gets the same objects as "A" and at approximately the same time.

You can learn more about this in:
Tags:
Run Edit